﻿using System.Data;

namespace LeetCodeProject._0001_1000._0001;

public class Solution
{
    // 两数之和
    public int[] TwoSum(int[] nums, int target)
    {
        for (int i = 0; i < nums.Length; i++)
        {
            for (int j = i + 1; j < nums.Length; j++)
            {
                if (nums[i] + nums[j] == target)
                    return new int[] { i, j };
            }
        }

        return null;
    }
    public int[] TwoSum2(int[] nums, int target)
    {
        for (int i = 0; i < nums.Length; i++)
        {
            var nj = target - nums[i];
            //Array.IndexOf:从数组中查找指定对象的下标
            //nj:表示要查找的对象
            //i+1:表示查找的开始位置
            var j = Array.IndexOf(nums, nj, i + 1);
            if (j >= 0)
            {
                return new int[] { i, j };
            }
        }

        return Array.Empty<int>();
    }
    public int[] TwoSum3(int[] nums, int target)
    {
        var map = new Dictionary<int, int>(nums.Length);
        for (int i = 0; i < nums.Length; i++)
        {
            map[nums[i]] = i;
        }

        for (int i = 0; i < nums.Length; i++)
        {
            var nj = target - nums[i];
            if (map.ContainsKey(nj))
            {
                var j = map[nj];
                if (j > i)
                {
                    return new[] { i, j };
                }
            }
        }

        return Array.Empty<int>();
    }
}